package com.senmo.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.senmo.activity.ChannelListActivity;
import com.senmo.adapter.ImagePagerAdapter;
import com.senmo.base.BaseFragment;
import com.senmo.data.DeviceDataProvider;
import com.senmo.model.DeviceModel;
import com.senmo.senmo.R;

import java.util.ArrayList;
import java.util.List;

import lx.af.adapter.AbsViewHolder;
import lx.af.adapter.AbsViewHolderAdapter;
import lx.af.utils.ViewInject.ViewInject;
import lx.af.utils.ViewUtils.ViewPagerAutoFlipper;
import lx.af.view.SwipeRefresh.SwipeRefreshLayout;
import lx.af.view.SwipeRefresh.SwipeRefreshListLayout;
import com.viewpagerindicator.PageIndicator;

/**
 * author: lx
 * date: 16-1-5
 */
public class MainTabDevice extends BaseFragment implements
        SwipeRefreshLayout.OnRefreshListener,
        AdapterView.OnItemClickListener,
        DeviceDataProvider.DeviceListCallback {

    @ViewInject(id = R.id.fragment_device_list, itemClick = "onItemClick")
    private ListView mListView;
    @ViewInject(id = R.id.fragment_device_refresh_layout)
    private SwipeRefreshListLayout mSwipeRefreshLayout;
    private View mHeaderView;
    private ViewPager mHeaderViewPager;
    private PageIndicator mHeaderPagerIndicator;

    private DeviceAdapter mAdapter;
    private ViewPagerAutoFlipper mPagerFlipper;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mHeaderView = inflater.inflate(R.layout.header_device_list, null);
        mHeaderViewPager = (ViewPager) mHeaderView.findViewById(R.id.device_list_header_pager);
        mHeaderPagerIndicator = (PageIndicator)
                mHeaderView.findViewById(R.id.device_list_header_pager_indicator);
        return inflater.inflate(R.layout.fragment_tab_device, null);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mSwipeRefreshLayout.setOnRefreshListener(this);
        mAdapter = new DeviceAdapter(getActivity());
        mListView.addHeaderView(mHeaderView);
        mListView.setAdapter(mAdapter);
        refreshHeaderView();
        DeviceDataProvider.getInstance().getDeviceList(false, this);
    }

    @Override
    public void onResume() {
        super.onResume();
        if (mAdapter != null){
            mAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        String device_id = mAdapter.getItem(i - 1).device_id;
        Intent intent = new Intent(getActivity(), ChannelListActivity.class);
        intent.putExtra("device_id", device_id);
        startActivity(intent);
    }

    @Override
    public void onGetDeviceList(ArrayList<DeviceModel> list) {
        mSwipeRefreshLayout.setRefreshing(false);
        if (list != null && list.size() != 0) {
            mAdapter.addAll(list);
        }
    }

    @Override
    public void onRefresh() {
        DeviceDataProvider.getInstance().getDeviceList(true, this);
    }

    private void refreshHeaderView() {
        List<String> uris = new ArrayList<>(4);
        uris.add("assets://ad1.jpg");
        uris.add("assets://ad2.jpg");
        uris.add("assets://ad3.jpg");
        uris.add("assets://ad4.jpg");
        mHeaderViewPager.setAdapter(new ImagePagerAdapter(uris));
        mHeaderPagerIndicator.setViewPager(mHeaderViewPager);
        if (mPagerFlipper == null) {
            mPagerFlipper = ViewPagerAutoFlipper.newInstance(mHeaderViewPager).setInterval(3000);
            mPagerFlipper.start();
        } else {
            mPagerFlipper.reset();
        }
    }

    private static class DeviceAdapter extends AbsViewHolderAdapter<DeviceModel> {

        public DeviceAdapter(Context context) {
            super(context);
        }

        @Override
        public View createItemView(Context context) {
            return View.inflate(context, R.layout.item_device, null);
        }

        @Override
        public AbsViewHolder<DeviceModel> createViewHolder(View itemView) {
            return new ViewHolder(itemView);
        }

        private static class ViewHolder extends AbsViewHolder<DeviceModel> {
            @ViewInject(id = R.id.item_device_name)
            TextView name;
            @ViewInject(id = R.id.item_device_no)
            TextView no;

            ViewHolder(View root) {
                super(root);
            }

            public void setData(DeviceModel device) {
                name.setText(device.device_name);
                no.setText(device.device_id);
            }
        }
    }


}
